<template>
	<view class="container">
		<view class="info">
			<view class="title">{{ title }}</view>
			<view class="time">{{ remark }}</view>
			<view class="time">{{ start_time | formatDate }}</view>
			<view class="desc" v-for="(item, index) in select" :key="index">
				<view>座位号：
					<!--          <text class="font-color-red">1号场B区</text>-->
					{{item.region_number}}区{{item.row_code}}排{{item.col_code}}号
				</view>
				<view>¥{{ item.price }}</view>
			</view>

		</view>


		<view class="info">
			<view class="desc">
				<view>验票方式：二维码验票</view>
			</view>
			<view class="form-item">
				<view class="form-label">购票人</view>
				<input class="form-input" placeholder="请填写购票人姓名" type="text" v-model="user_name">
			</view>
			<view class="form-item">
				<view class="form-label">手机号</view>
				<input maxlength="11" class="form-input" placeholder="请填写购票人手机号" type="number" v-model="phone">
			</view>
			<view class="form-item">
				<view class="form-label">身份证</view>
				<input class="form-input" placeholder="请填写身份证号码" type="idcard" v-model="card_number">
			</view>
		</view>

		<view class="info">
			<view class="desc">
				<view>价格</view>
				<view class="price-container">
					<template v-if="is_group">
						<view class="price">¥{{ (total*group_rate).toFixed(2) }}</view>
						<view class="no-price">¥{{ total }}</view>
					</template>
					<template v-else>
						<view class="price">¥{{ total}}</view>
					</template>
				</view>
			</view>
		</view>
		<view class="footer">
			<view>
				应付款
				<text class="font-color-red">￥{{(total*group_rate).toFixed(2)}}</text>
			</view>
			<view class="btn" @click="onSubmit">立即支付</view>
		</view>
		<payment :payMode='payMode' :pay_close="pay_close" @onChangeFun='onChangeFun' :order_id="pay_order_id"
			:totalPrice='total'></payment>
	</view>
</template>

<script>
	import {
		performPay
	} from "@/api/2_activity";
	import {
		showConfirmOrder,
		ticketPay
	} from "@/api/show";
	import payment from '@/components/payment';
	const app = getApp()
	export default {
		components: {
			payment,
		},
		data() {
			return {
				id: '',
				section_id:'',
				title: '',
				remark: '',
				start_time: '',
				select: [],
				select_site: '',
				user_name: '', //取票人姓名
				phone: '', //取票人电话
				card_number: '', //取票人身份证
				is_group:false,
				group_rate:1,
				is_discount:'1',//打折否
				payMode: [{
						name: "微信支付",
						icon: "icon-weixinzhifu",
						value: 'wechat',
						title: '微信快捷支付',
						payStatus: 1
					},
					{
						name: "支付宝支付",
						icon: "icon-zhifubao",
						value: 'alipay',
						title: '支付宝支付',
						payStatus: app.globalData.alipay_open
					},
					{
						name: "余额支付",
						icon: "icon-yuezhifu",
						value: 'balance',
						title: '可用余额:',
						number: 0,
						payStatus: app.globalData.yue_pay_status
					}
				],
				pay_close: false,
				pay_order_id: '',
				handle:false
			}
		},
		onLoad(options) {
			this.id = options.id
			this.title = options.title
			this.remark = options.remark
			this.start_time = options.start_time
			this.is_discount = options.is_discount
			this.select = JSON.parse(options.select)
			console.info(this.is_discount)
			if(this.is_discount == '1' || this.is_discount == 1){
				console.info("打折");
				if(this.select.length >= 5){
					this.is_group = true;
					if(this.select.length >= 5 && this.select.length < 10){
						this.group_rate = 0.95;
						//5-10张95折
					}else if(this.select.length >= 10){
						this.group_rate = 0.9;//10张以上9折
					}
				}else{
					this.is_group = false;
				}
			}else{
				console.info("不打折");
			}
			
			this.select_site = options.select_site
		},
		computed: {
			total() {
				return this.select.reduce((a, b) => {

					return +a + +b.price
				}, 0)
			}
		},
		methods: {
			/**
			 * 关闭支付组件
			 * 
			 */
			payClose: function() {
				this.pay_close = false;
			},
			/**
			 * 事件回调
			 * 
			 */
			onChangeFun: function(e) {
				let opt = e;
				let action = opt.action || null;
				let value = opt.value != undefined ? opt.value : null;
				(action && this[action]) && this[action](value);
			},
			/**
			 * 打开支付组件
			 * 
			 */
			goPay: function(item) {
				// console.log(order_id)
				this.$set(this, 'pay_close', true);
				this.order_id = item.order_id;
				this.pay_order_id = item.order_id.toString()
				// this.$set(this, 'pay_order_id', );
				this.$set(this, 'totalPrice', item.pay_price);
			},
			/**
			 * 支付成功回调
			 * 
			 */
			pay_complete: function() {
				this.loadend = false;
				this.page = 1;
				this.tab(this.status)
				this.pay_close = false;
				this.pay_order_id = '';
				// this.getOrderData();
				// this.getOrderList();
			},
			/**
			 * 支付失败回调
			 * 
			 */
			pay_fail: function() {
				this.pay_close = false;
				this.pay_order_id = '';

			},
			onSubmit() {
				let _this = this
				if (!_this.phone || _this.phone.length != 11) {
					_this.$util.Tips({
						title: '请输入正确的手机号'
					});
					return;
				}
				
				if(!this.card_number || this.card_number.length < 16){
					_this.$util.Tips({
						title: '请输入正确的身份证号'
					});
					return
				}
				if (this.select.length == 0) {
					_this.$util.Tips({
						title: '请选择座位'
					});
					return;
				}
				
				if(_this.handle){
					return;
				}
				uni.showLoading({
					title:'处理中...'
				})
				_this.handle = true;
				showConfirmOrder({
					select_id: this.select_site,
					user_name: this.user_name,
					phone: this.phone,
					card_number: this.card_number,
					total:this.total
				}).then(res => {
						
					   if(res.data['order_sn'] == undefined || res.data['id'] == undefined || res.data['id'] == ""){
						   _this.handle = false;
						   uni.hideLoading();
						   this.$util.Tips({
						   	title: res.message
						   });
						   return;
					   }
						// 支付
						ticketPay({
							order_sn: res.data.order_sn,
							pay_type: 2
						}).then(res2 => {
								let config = res2.data.result.config
								uni.requestPayment({
									timeStamp: config.timestamp,
									nonceStr: config.nonceStr,
									package: config.package,
									signType: config.signType,
									paySign: config.paySign,
									success: (res1) => {
										_this.handle = false;
										uni.hideLoading();
										this.$util.Tips({
													title: '支付成功',
													icon: 'success'
												})
												// , {
												// 	tab: 5,
												// 	url: `/pages/2_development/show_buy_ticket/result?type=success&title=${this.title}&id=${res.data.id}&order_sn=${res.data.order_sn}`
												// }
												setTimeout(()=>{
													uni.reLaunch({
														url:`/pages/2_development/show_buy_ticket/result?type=success&title=${this.title}&id=${res.data.id}&order_sn=${res.data.order_sn}`
													})
												},500)
												// uni.navigateBack({
												// 	delta: 2
												// })
											},
											fail: () => {
												_this.handle = false;
												uni.hideLoading();
												this.$util.Tips({
													title: '支付失败'
												});
												uni.navigateBack({
													delta: 2
												})
												// , {
												// 	tab: 5,
												// 	url: `/pages/2_development/show_buy_ticket/result?type=fail&title=${this.title}`
												// }
											},
											complete: (res) => {
												_this.handle = false;
												uni.hideLoading();
												if (res.errMsg == 'requestPayment:cancel') return this
													.$util
													.Tips({
														title: '取消支付'
													});
											}
									})
								}).catch(res => {
									_this.handle = false;
									uni.hideLoading();
									this.$util.Tips({
										title: res.message
									});
								})
						}).catch(res => {
							_this.handle = false;
							uni.hideLoading();
							this.$util.Tips({
								title: res.message
							});
					})
				}//end submit
				
			}
		}
</script>

<style lang="scss">
	.container{
		display: flex;
		flex-direction: column;
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);  
		padding-bottom: env(safe-area-inset-bottom); 
	}
	.info {
		background: #fff;
		padding: 20rpx 0;
		margin-bottom: 20rpx;
	}

	.title {
		font-size: 32rpx;
		font-weight: 700;
		margin: 20rpx 30rpx 0;
	}

	.time {
		margin: 20rpx 30rpx;
		font-size: 24rpx;
		color: #82848f;
	}

	.desc {
		padding: 0 30rpx;
		font-size: 28rpx;
		color: #282828;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		
		.price-container{
			display: flex;
			flex-direction: row;
			.price{
				font-size: 30rpx;
				color: #FF3700;
			}
			.no-price{
				font-size: 30rpx;
				color: #000000;
				text-decoration: line-through;
				margin-left: 10rpx;
			}
		}
	}
	.tip-info{
		background: #fff;
		padding: 30rpx 30rpx;
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: column;
		.tip{
			font-size: 28rpx;
			margin-bottom: 20rpx;
		}
	}
	.aggree{
		display: flex;
		flex-direction: row;
		align-items: center;
		.red{
			font-size: 28rpx;
			color: #e93323;
		}
	}

	.form-item {
		display: flex;
		align-items: center;
		height: 88rpx;
		padding: 0 30rpx;

		.form-label {
			width: 100rpx;
			font-size: 28rpx;
			color: #282828;
		}

		.form-input {
			flex: 1;
			height: 88rpx;
		}
	}

	.footer {
		width: 100%;
		// height: 100rpx;
		background-color: #fff;
		padding: 10rpx 30rpx;
		font-size: 28rpx;
		color: #333;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-bottom: calc(10rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(10rpx + env(safe-area-inset-bottom));

		.price {
			font-size: 28rpx;
			color: #fff;
		}

		.btn {
			font-size: 30rpx;
			color: #fff;
			width: 240rpx;
			height: 70rpx;
			background-color: #e93323;
			border-radius: 50rpx;
			text-align: center;
			line-height: 70rpx;
		}
	}
</style>
